package v1

import (
	"fmt"
	"ginblog/model"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 新增关注
func AddFan(c *gin.Context)  {

	// 引入结构体
	var data model.Fan
	_ = c.ShouldBindJSON(&data)
	uid,_ := c.Get("userid")
	data.Uid = uid.(int)

	fmt.Printf("u111:%#v\n",data.FollowUid)

	// 查询用户是否关注过用户
	userFollow,code := model.IsFollow(data.Uid,data.FollowUid)

	// 关注
	if data.Status == 0{
		if code == errmsg.ERROR{
			code = model.CreateFan(&data) // 没有数据，关注成功
			model.IncrUserFollow(data.Uid) // 关注数加1
			model.IncrUserFan(data.FollowUid) // 粉丝数加1
		}else{
			if userFollow.Status == 1 {
				model.SaveFan(&data,data.Uid) //
				model.IncrUserFollow(data.Uid) // 关注数加1
				model.IncrUserFan(data.FollowUid) // 粉丝数加1
			}
		}
	}else{
		// 取消关注
		if code == errmsg.SUCCSE{
			if userFollow.Status == 0{
				model.SaveFan(&data,data.Uid) //
				model.SubUserFollow(data.Uid) // 关注数-1
				model.SubUserFan(data.FollowUid) // 粉丝数-1
			}
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"status":code,
		"message":errmsg.GetErrorMsg(code),
	})

}

//  查询我的关注列表
func GetFollowList(c *gin.Context){
	uid,_ := c.Get("userid")
	pageSize,_ := strconv.Atoi(c.DefaultQuery("pagesize", "10"))
	pageNum,_ := strconv.Atoi(c.DefaultQuery("pagenum", "1"))
	data,code,total := model.GetFollowList(uid.(int), pageSize, pageNum)

	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"total":total,
		"message":errmsg.GetErrorMsg(code),
	})
}

//  查询我的粉丝列表
func GetFanList(c *gin.Context){
	uid,_ := c.Get("userid")
	pageSize,_ := strconv.Atoi(c.DefaultQuery("pagesize", "10"))
	pageNum,_ := strconv.Atoi(c.DefaultQuery("pagenum", "1"))
	data,code,total := model.GetFanList(uid.(int), pageSize, pageNum)

	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"total":total,
		"message":errmsg.GetErrorMsg(code),
	})
}



